<?php
session_start();
require_once("config.php");

//require_once("Cache/Lite.php");
require_once("simplexml44-0_4_4/class/IsterXmlSimpleXMLImpl.php");
require_once("Smarty-2.6.18/libs/Smarty.class.php");

require_once("ArtistWiki.php");
require_once("Inputlist.php");
require_once("ManualInput.php");
require_once("Lastfm.php");
require_once("Info.php");
require_once("MatchInfo.php");
require_once("Database.php");
require_once("TagCloud.php");
require_once("Venue.php");
require_once("VenueList.php");
require_once("UserMusic.php");

//$Cache_Lite = new Cache_Lite($CONFIG['cache_lite_options']);

$smarty = new Smarty();
$smarty->compile_check = true;
$smarty->debugging = false;

$tagCloud = new TagCloud();
$tags = $tagCloud->getCloud();
$smarty->assign("tags", $tags);

$venueList = new VenueList();
$venues_festivals = $venueList->getFestivals();
$venues_gigs = $venueList->getGigs();
$smarty->assign("venues_festivals", $venues_festivals);
$smarty->assign("venues_gigs", $venues_gigs);

// Needs to be cleaned up.

Venue::parseRSS("http://www.samfundet.no/rss/rss.php?type[]=1");

if (!empty($_GET['submit']) && !empty($_GET['venue_id'])) {
    // Input from the venue-site
    $lineup = Venue::parse($_GET['venue_id']);
    
    // Input from the user
    if(!empty($_GET['username'])) {
        $input = "lastfm profile <em>".$_GET['username']."</em>";
        $userArtists = Lastfm::topArtists($_GET['username']);
    } elseif(!empty($_GET['inputbox_artists'])) {
        $input = "artists <em>".$_GET['inputbox_artists']."</em>";
        $userArtists = ManualInput::split_string($_GET['inputbox_artists']);
    } elseif(!empty($_GET['tag'])) {
        $input = "tag <em>".$_GET['tag']."</em>";
        $userArtists = ManualInput::read_tag_xml($_GET['tag']); 
        // Update tag-weight
        if(empty($_SESSION['tag_clicked'])) {
            TagCloud::updateWeight($_GET['tag']);
            $_SESSION['tag_clicked'] = 1;   
        }
    } elseif(!empty($_GET['tagTextField'])) {
        $input = "custom tag <em>".$_GET['tagTextField']."</em>";
        $userArtists = ManualInput::read_tag_xml($_GET['tagTextField']);
        if($userArtists) {
            TagCloud::addTag($_GET['custom_tag']);
        }
    } else {
        // Data from Step 1 is missing
    }
    
    // Print the list ranked linup
    if (is_array($userArtists) && is_array($lineup)) {
        $venue = Venue::getVenue($_GET['venue_id']);
        $smarty->assign("venueMeta", $venue['meta']);
        $resultLineup = Lastfm::match($lineup, $userArtists);
        $smarty->assign("test", $resultLineup[6]->artist);
        $smarty->assign("reultLineup", $resultLineup);
        $smarty->assign("input", $input);
    }
}

$smarty->display('index.tpl');
Info::display();
Info::clear();
?>